package Ui;

import java.awt.Font;
import java.awt.Point;

import javax.swing.JButton;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import Controller.GameController;
import Messaging.Notification;
import Messaging.NotificationManager;

/* 
 * @author Roch San Buenaventura
 */
public class GameButtonPanel extends JPanel 
{
	
	JButton purchase_, mortgage_, trade_, endTurn_, quitGame_;
	
	Font buttonFont;
	
	GameController controller_;
	
	int buttonX = 175, buttonY = 40;
	int frameWidth = buttonX * 5;
	int frameHeight = buttonY;
	
	private boolean purchaseState_, endTurnState_, tradeState_, mortgageState_;
	
	private int currentlySavedState = 0;
	
	static final long serialVersionUID = 11;
	
	public GameButtonPanel(GameController controller) 
	{
		
		controller_ = controller;
		
		buttonFont = new Font("SansSerif", Font.BOLD, 14);
		
		this.setSize(frameWidth, frameHeight);
		
		this.setLayout(null);	

		purchase_ = new JButton("Purchase");
		purchase_.setSize(125,40);
		purchase_.addActionListener(controller_);
		purchase_.setEnabled(false);
		
		mortgage_ = new JButton("Properties");
		mortgage_.setSize(125,40);
		mortgage_.addActionListener(controller_);
		mortgage_.setEnabled(true);
		
		endTurn_ = new JButton("End Turn");
		endTurn_.setSize(125,40);
		endTurn_.addActionListener(controller_);
		endTurn_.setEnabled(false);
		
		quitGame_ = new JButton("Quit Game");
		quitGame_.setSize(125,40);
		quitGame_.addActionListener(controller_);
		
		trade_ = new JButton("Trade");
		trade_.setSize(125,40);
		trade_.addActionListener(controller_);
		trade_.setEnabled(true);
		
		this.add(purchase_);
		this.add(mortgage_);
		this.add(endTurn_);
		this.add(quitGame_);
		this.add(trade_);
		
		DisplayAssembler.getInstance().addComponent(purchase_, new Point(700, 425), JLayeredPane.PALETTE_LAYER);
		DisplayAssembler.getInstance().addComponent(mortgage_, new Point(825, 425), JLayeredPane.PALETTE_LAYER);
		DisplayAssembler.getInstance().addComponent(endTurn_, new Point(700, 465), JLayeredPane.PALETTE_LAYER);
		DisplayAssembler.getInstance().addComponent(trade_, new Point(825, 465), JLayeredPane.PALETTE_LAYER);
		DisplayAssembler.getInstance().addComponent(quitGame_, new Point(825, 700), JLayeredPane.PALETTE_LAYER);

		NotificationManager.getInstance().addObserver(Notification.DONE_ROLLING, this, "playerState");
		NotificationManager.getInstance().addObserver(Notification.UNOWNED_PROPERTY, this, "enablePurchase");
		NotificationManager.getInstance().addObserver(Notification.END_TURN, this, "rollingState");
		NotificationManager.getInstance().addObserver(Notification.DISABLE_PURCHASE, this, "disablePurchase");
		NotificationManager.getInstance().addObserver(Notification.SHOW_CARD, this, "setAllDisabled");
		NotificationManager.getInstance().addObserver(Notification.REMOVE_CARD, this, "setEnabled");
		NotificationManager.getInstance().addObserver(Notification.END_TURN_EARLY, this, "playerState");
	}

	public void playerState()
	{
		endTurn_.setEnabled(true);
	}
	
	public void enablePurchase() {
		purchase_.setEnabled(true);
	}
	
	public void disablePurchase()
	{
		purchase_.setEnabled(false);
	}
	
	public void rollingState()
	{
		purchase_.setEnabled(false);
		endTurn_.setEnabled(false);
		
		mortgage_.setEnabled(true);
		trade_.setEnabled(true);
	}
	
	public void setAllDisabled() 
	{
		if (currentlySavedState == 0)
		{
			purchaseState_ = purchase_.isEnabled();
			purchase_.setEnabled(false);
		
			mortgageState_ = mortgage_.isEnabled();
			mortgage_.setEnabled(false);
		
			endTurnState_ = endTurn_.isEnabled();
			endTurn_.setEnabled(false);
			
			tradeState_ = trade_.isEnabled();
			trade_.setEnabled(false);
		}
		
		currentlySavedState++;
	}
	
	public void setEnabled()
	{
		if (currentlySavedState == 1)
		{
			purchase_.setEnabled(purchaseState_);
			mortgage_.setEnabled(mortgageState_);
			endTurn_.setEnabled(endTurnState_);
			trade_.setEnabled(tradeState_);
		}
		
		currentlySavedState--;
	}
}
